package de.tabuma.shopping.controller;

import de.tabuma.shopping.entity.*;
import de.tabuma.shopping.logik.*;
import de.tabuma.shopping.view.*;
import de.tabuma.lib.tabumacontroller;
import java.io.*;
import java.util.logging.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.google.appengine.api.users.*;

/**
 *
 * @author $Author: alefen $
 * @version $Revision: 1.2 $
 * @since $Date: 2009/07/21 19:37:32 $
 */
public class details extends tabumacontroller {

    private static Logger log =  Logger.getLogger(details.class.getName());

    private String orderNav;
    private LicensePackageFacade licensePackageFacade;
    private String url;

    public details(String servlet, Hashtable parameter, String orderNav, String url, LicensePackageFacade licensePackageFacade) {
        super(servlet, parameter);
        this.orderNav = orderNav;
        this.url = url;
        this.licensePackageFacade = licensePackageFacade;
    }



    public void navigate(ServletContext context, HttpServletRequest request, HttpServletResponse response, PrintWriter out)  {

        log.setLevel(Level.ALL);

        forwardToPayPal forwardToPayPalController = new forwardToPayPal(this.getServlet(), this.getParameter(), orderNav);

        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();

        String value = request.getParameter(de.tabuma.shopping.constants.ORDER_NR);
        if(value==null) {
            value = (String) request.getAttribute(de.tabuma.shopping.constants.ORDER_NR);
        }
        int intValue = Integer.valueOf(value);
        Order myOrder = null;
        if(userService.isUserAdmin()) {
            myOrder = OrderFacade.getOrder(intValue);
        }
        else {
            myOrder = OrderFacade.getOrder(intValue);
        }

        if(request.getParameter(de.tabuma.shopping.constants.ORDER_STARTPAYPAL)!=null) {
            log.fine("Start pay pal.");
            String token = PayPalFacade.getPayPalTokenSetExpressCheckout(myOrder, this.getParameter(),orderNav, this.licensePackageFacade , this.getServlet(), url);
            log.fine("PayPal token is "+token);
            request.setAttribute(de.tabuma.shopping.constants.ORDER_TOKEN, token);
            request.setAttribute(de.tabuma.shopping.constants.URL, de.tabuma.shopping.constants.payPalUrl+"?cmd=_express-checkout&token="+token);
            forwardToPayPalController.navigate(context, request, response,out);
            return;
        }

        request.setAttribute(de.tabuma.shopping.constants.ORDER,myOrder);
        List<LicensePackage> list = licensePackageFacade.getAllLicensePackageList();
        request.setAttribute(de.tabuma.shopping.constants.ORDER_LICENSEPACKAGES,list);

        List<OrderLicensePackageRelation> list1 = OrderLicensePackageRelationFacade.getList(intValue);
        request.setAttribute(de.tabuma.shopping.constants.ORDER_LICENSEPACKAGERELATION_LIST, list1);

        request.setAttribute(this.orderNav,de.tabuma.shopping.constants.ORDERNAV_DETAILS);
    }

}
